//
//  WmcInputItemView.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/14.
//

import RxCocoa
import RxSwift
import UIKit

class WmcInputItemView: WmcBaseView {
    /// 左边标题
    var leftTitle: String? {
        didSet {
            guard let leftTitle = leftTitle else {
                return
            }
            leftTitleLabel.text = leftTitle
        }
    }

    /// 占位提示
    var placeholder: String? {
        didSet {
            guard let placeholder = placeholder else {
                return
            }
            self.inputTextfield.placeholder = placeholder
        }
    }

    override func makeUI() {
        addSubview(leftTitleLabel)
        addSubview(inputTextfield)
        addSubview(lineView)

        leftTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.left.equalTo(16)
            make.bottom.equalTo(-16)
            make.height.equalTo(26)
            make.width.equalTo(85)
        }

        inputTextfield.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(leftTitleLabel.snp.right).offset(24)
            make.right.equalTo(-16)
        }

        lineView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }

        // self.inputTextfield.setCursorPosition(input: self.inputTextfield, position: 1)
    }

    override func bindViewModel() {
        /// 绑定自定义扩展，clear btn 是否显示
        inputTextfield.rx.text.map(customTransform(text:)).bind(to: inputTextfield.rx.viewMode).disposed(by: rx.disposeBag)

        /// 代理事件
        inputTextfield.rx.controlEvent(.editingDidBegin).bind { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.lineView.backgroundColor = UIColor.wmc_mainTheme
        }.disposed(by: rx.disposeBag)

        inputTextfield.rx.controlEvent(.editingDidEnd).bind { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.lineView.backgroundColor = UIColor.wmc_grayH19_F5F5F5
        }.disposed(by: rx.disposeBag)
    }

    lazy var leftTitleLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.wmc_grayH2_323233
        view.font = 17.yp.uiFont
        return view
    }()

    lazy var inputTextfield: UITextField = {
        let view = UITextField()
        view.modifyClearButtonWithImage(image: UIImage(named: "input_clear_icon"))
        view.rightViewMode = .unlessEditing
        return view
    }()

    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.wmc_grayH19_F5F5F5
        return view
    }()
}

extension WmcInputItemView {
    /// 为了不让语句过长，闭包写成函数形式
    /// - Parameter text: text description
    /// - Returns: description
    func customTransform(text: String?) -> UITextField.ViewMode {
        return (text ?? "").count > 0 ? UITextField.ViewMode.whileEditing : UITextField.ViewMode.never
    }
}

extension Reactive where Base: WmcInputItemView {
    var lineColor: Binder<UIColor?> {
        return Binder(base) { itemView, color in
            guard let color = color else { return }
            itemView.lineView.backgroundColor = color
        }
    }
}

extension UITextField {
    /// 修改 clear button
    /// - Parameter image: image
    func modifyClearButtonWithImage(image: UIImage?) {
        let clearButton = UIButton(type: .custom)
        clearButton.setImage(image, for: .normal)
        clearButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        clearButton.contentMode = .scaleAspectFit
        clearButton.addTarget(self, action: #selector(UITextField.clear(sender:)), for: .touchUpInside)
        rightView = clearButton
        rightViewMode = .whileEditing
    }

    @objc func clear(sender _: AnyObject) {
        text = ""
        sendActions(for: .editingChanged)
    }

    func setCursorPosition(input: UITextField, position: Int) {
        let position = input.position(from: input.beginningOfDocument, offset: position)!
        input.selectedTextRange = input.textRange(from: position, to: position)
    }
}

extension UITextField {
    /// 限制输入字符个数
    /// - Parameter count: 允许输入字符个数
    /// - Returns: description
    func limitInputCharactersCount(count: Int) {
        rx.text.orEmpty.map { $0.count > count }.bind { [weak self] valid in
            guard let weakSelf = self else { return }
            if valid {
                let index = weakSelf.text!.index(weakSelf.text!.startIndex, offsetBy: count)
                weakSelf.text = String(weakSelf.text![..<index])
            }
        }.disposed(by: rx.disposeBag)
    }
}

extension Reactive where Base: UITextField {
    var viewMode: Binder<UITextField.ViewMode> {
        return Binder(base) { tf, vMode in
            tf.rightViewMode = vMode
        }
    }
}

/// 不带左边标题的 输入控件

class WmcSpecificInputItemView: WmcInputItemView {
    override func makeUI() {
        addSubview(inputTextfield)
        addSubview(lineView)

        inputTextfield.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.bottom.equalTo(-16)
            make.height.equalTo(26)
            make.left.equalTo(16)
            make.right.equalTo(-16)
        }

        lineView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
}
